package cn.edu.ldh.toolapp.provider;

import cn.edu.ldh.toolapp.ResourceTable;
import cn.edu.ldh.toolapp.entity.NoteItem;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.*;
import java.util.List;

public class NoteItemProvider extends BaseItemProvider {

    private List<NoteItem> list;
    private AbilitySlice slice;

    public NoteItemProvider(List<NoteItem> list, AbilitySlice slice) {
        this.list = list;
        this.slice = slice;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int i) {
        if(list != null && i >= 0 && i < list.size()) {
            return list.get(i);
        }
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        final Component cpt;
        if(component == null) {
            cpt = LayoutScatter.getInstance(slice).parse(ResourceTable.Layout_item_note, null, false);
        } else {
            cpt = component;
        }
        NoteItem noteItem = list.get(i);
        //item组件赋值
        Text title_text = (Text) cpt.findComponentById(ResourceTable.Id_item_note_title);
        Text date_text = (Text) cpt.findComponentById(ResourceTable.Id_item_note_time);
        title_text.setText(noteItem.getNoteTitle());
        date_text.setText(noteItem.getNoteDate());
        return cpt;
    }
}
